package indi.kider.server.appclient.api;

import indi.kider.common.model.apabus.Bus;
import indi.kider.common.model.apabus.Dispatch;
import indi.kider.common.model.apabus.Evaluate;
import indi.kider.common.model.apabus.ServiceDelivery;
import indi.kider.server.appclient.service.related.ApaBusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shixj on 2018/5/31.
 */
@RestController
@RequestMapping(value = "service", produces = "application/json;charset=UTF-8")
public class AppClientServiceController {
    @Autowired
    private ApaBusService apaBusService;
    //发起服务 1
    //查询服务列表 1
    //查看服务详情 1
    //评价服务 1
    //删除服务1
    //取消服务1

    /**
     * @api {POST} service/serviceDelivery
     * @apiGroup app-client
     * @apiDescription 服务提供
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service/serviceDelivery
     * {"busType":1,"busdeis":"电灯坏了","rentId":1}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PostMapping("/serviceDelivery")
    public CommonResponse serviceDelivery(@RequestBody ServiceDelivery serviceDelivery) {
        return apaBusService.serviceDelivery(serviceDelivery);
    }

    /**
     * @api {PUT} service/workOrderReceipt
     * @apiGroup app-client
     * @apiDescription 服务派单回执
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service/workOrderReceipt
     * {"busId":1,"result":"取消"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/workOrderReceipt")
    public CommonResponse workOrderReceipt(@RequestBody Dispatch dispatch) {
        return apaBusService.workOrderReceipt(dispatch);
    }

    /**
     * @api {POST} service/list
     * @apiGroup app-client
     * @apiDescription 根据字段查询获取服务列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service/list
     * {"num":"0","size":"8","query":{"busType":1,"state":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"busType":null,"community":null,"des":null,"employeeId":null,"evaluation":null,"evsu":null,"evsuTime":null,"houses":null,"housesId":null,"id":null,"insTime":null,"result":null,"resutlTime":null,"state":null,"tenantId":null,"tenantName":null,"tenantTel":null,"waiter":null,"waiterTime":null}],"total":1,"size":0}}
     */
    @PostMapping("/list")
    public CommonResponse getPageServiceList(@RequestBody PageRequest<Bus> request, HttpServletRequest httpServletRequest) {
        try {
            Long tenantId = Long.valueOf(httpServletRequest.getHeader("id"));
            request.getQuery().setTenantId(tenantId);
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse("请检查请求header！");
        }
        return apaBusService.getPageServiceList(request);
    }

    /**
     * @api {GET} service/{busId}
     * @apiGroup app-client
     * @apiDescription 根据字段查询获取服务
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service/{busId}
     * {"num":"0","size":"8","query":{"busType":1,"state":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"busType":null,"community":null,"des":null,"employeeId":null,"evaluation":null,"evsu":null,"evsuTime":null,"houses":null,"housesId":null,"id":null,"insTime":null,"result":null,"resutlTime":null,"state":null,"tenantId":null,"tenantName":null,"tenantTel":null,"waiter":null,"waiterTime":null}],"total":1,"size":0}}
     */
    @PostMapping("/{busId}")
    public CommonResponse getPageServiceList(@PathVariable Long busId) {
        PageRequest<Bus> pageRequest = new PageRequest();
        pageRequest.setSize(1);
        pageRequest.setNum(1);
        Bus bus = new Bus();
        bus.setId(busId);
        pageRequest.setQuery(bus);
        List<Bus> busList = (List<Bus>) apaBusService.getPageServiceList(pageRequest).getData().getRows();
        return CommonResponse.buildGetSuccessResponse(busList.get(0));
    }

    /**
     * @api {PUT} service/evaluate
     * @apiGroup app-client
     * @apiDescription 服务派单评分
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service/evaluate
     * {"busId":1,"evaluation":5,"evsu":"非常满意"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/evaluate")
    public CommonResponse evaluate(@RequestBody Evaluate evaluate) {
        return apaBusService.evaluate(evaluate);
    }

    /**
     * @api {PUT} /
     * @apiGroup app-client
     * @apiDescription 删除服务delState 2/取消服务delState 3
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/service
     * {"id":1,"delState":2}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}
     */
    @PutMapping("")
    public CommonResponse delBus(@RequestBody Bus bus) {
        return apaBusService.delBus(bus);
    }
}
